

import DataHelper from "./DataHepler"
import ItemData from "../model/ItemData" //数据模型类
import Category from '@/model/CateEnum'

class ActionHelper {
    //1。负责数据处理
    dataHelper: DataHelper = new DataHelper("memoData", "id")
    memoList: Array<ItemData>
    // memoList2?:ItemData[] 定义数据的第二种方式


    //构造函数，读取本地数据，并设置给memoList: Array<ItemData>
    constructor() {
        this.memoList = this.readData()
    }


    //读取本地数据，并返回ItemData 类型数组
    readData(): Array<ItemData> {
        //读取本地的object数组
        let arr = this.dataHelper.readData()
        //转成obejct数组
        let arrItem = arr.map((ele: any) => {
            let item: ItemData = new ItemData()
            item.id = ele.id
            item.categoryId = ele.categoryId
            item.title = ele.title
            item.content = ele.content
            item.createTime = ele.createTime
            return item
        })
        return arrItem
    }

    //2.负责业务处理

    //新增一条记录
    add(item: ItemData): number {
        //拿id
        item.id = this.dataHelper.addData(item)

        //添加
        this.memoList.push(item)
        //存
        this.dataHelper.saveData(this.memoList)

        return item.id
    }


    //修改一条记录
    edit(item: ItemData): void {
        let editItem: ItemData | undefined = this.memoList.find((ele) => {
            return ele.id == item.id
        })
        if (editItem) {
            editItem.categoryId = item.categoryId
            editItem.title = item.title
            editItem.content = item.content
            //保存
            this.dataHelper.saveData(this.memoList)
        }
    }

    //删除一条记录
    remove(id: number) {
        console.log("remove",id)
        console.log(this.memoList)
        let index = this.memoList.findIndex((item) => {
            return item.id == id
        })
        console.log(index)
        if (index < 0) return false
        this.memoList.splice(index, 1)
        //存
        this.dataHelper.saveData(this.memoList)
        return true
    }

    //
    getCategoryName(cateId:Category):string{
        const arrName=['工作','生活','学习'] 
        return arrName[cateId]
    }
}


export default ActionHelper





